import { Dict } from './interface'
import { UC } from '@/api/config/servicePort'
import http from '@/api'
import { ResPage, SelectOption } from '@/api/interface'

// 通过类别获取子项
export const listDictByType = (type: string) => {
  return http.get<SelectOption[]>(UC + `/dict/type`, { type })
}

// 应用列表
export function pageDict(query: Dict.Query) {
  return http.get<ResPage<Dict.Record>>(UC + `/dict/page`, query)
}

// 新增
export const addDict = (params: Partial<Dict.Form>) => {
  return http.post(UC + `/dict`, params)
}

// 新增子项
export const addDictItem = (params: Partial<Dict.ItemForm>) => {
  return http.post(UC + `/dict/item`, params)
}

// 编辑
export const updateDict = (params: Partial<Dict.Form>) => {
  return http.put(UC + '/dict', params)
}

// 编辑子项
export const updateDictItem = (params: Partial<Dict.ItemForm>) => {
  return http.put(UC + '/dict/item', params)
}

// 删除
export const removeDict = (id: string) => {
  return http.delete(UC + `/dict`, { id })
}

// 删除子项
export const removeDictItem = (id: string) => {
  return http.delete(UC + `/dict/item`, { id })
}

// 详情
export const getDict = (id: string) => {
  return http.get<Partial<Dict.Form>>(UC + '/dict', { id }, { loading: false })
}

// 详情子项
export const getDictItem = (id: string) => {
  return http.get<Partial<Dict.ItemForm>>(UC + '/dict/item', { id }, { loading: false })
}

// 检查类别是否存在
export function checkExist(query: Dict.CheckQuery) {
  return http.get<boolean>(UC + '/dict/exist', query, { loading: false })
}

// 通过类别获取子项
export const pageDictItem = (query: Dict.ItemQuery) => {
  return http.get<ResPage<Dict.ItemRecord>>(UC + `/dict/item/page`, query)
}

// 检查子项类别是否存在
export function checkItemExist(query: Dict.CheckItemQuery) {
  return http.get<boolean>(UC + '/dict/item/exist', query, { loading: false })
}

// 新增表单默认值
export const getDefault = (dictId: string) => {
  return http.get<Partial<Dict.Default>>(UC + '/dict/item/default', { dictId }, { loading: false })
}
